﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SimCMS.Manager;
using SimCMS.Model;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Cache;
using System.Drawing;


namespace SimCMS.Controllers
{
    public class SpiderDataController : Controller
    {

        public ISimchildobjManager SimchildobjManager { get; set; }
        public ISimobjattrManager SimobjattrManager { get; set; }
        public ISimobjsattrManager SimobjsattrManager { get; set; }
        public ISimFCategoryManager SimFCategoryManager { get; set; }
        public ISimFoodmenuManager SimFoodmenuManager { get; set; }

        public ActionResult index()
        {
            #region
            //var SimCobjModel = new Simchildobj();
            //var SValueList = new List<Simchildobjsvalue>();
            //var ss=  new Simchildobjsvalue() { SId = 1, SValue = "1" } ;
            //SValueList.Add(ss);
            //var valuelist = new List<Simchildobjvalue>();
            //var yy = new Simchildobjvalue() { AttrId = 1, AttrValue = "1", Unit = "ss" };
            //valuelist.Add(yy);
            //SimCobjModel.CName = "test";
            ////SimCobjModel.ObjId = 1;
            //SimCobjModel.Simchildobjsvalue = SValueList;
            //SimCobjModel.Simchildobjvalue = valuelist;
            //yy.Simchildobj = SimCobjModel;
            //ss.Simchildobj = SimCobjModel;
            //SimchildobjManager.Save(SimCobjModel);  
            //SendHttpDEMO();
            //http://fitness.39.net/food/list-1-1.html
            //var AttList = SimobjattrManager.LoadAll();
            //var SAttList = SimobjsattrManager.LoadAll();
            //for (int i = 1; i < 5; i++)
            //{
            //    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-1-"+i+".html");
            //    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"),1);
            //    foreach (var Model in List)
            //    {
            //        var objModels = SimCMS.Common.WebHelper.GetDetailInformation(Model, AttList.ToList(), SAttList.ToList());
            //        SimchildobjManager.Save(objModels);
            //    }
            //}
            #endregion
            //SavePageModel(1);
            //SavePageModel(2);
            //SavePageModel(3);
            //SavePageModel(4);
            //SavePageModel(5);
            //SavePageModel(6);
            //SavePageModel(7);
            //SavePageModel(8);
            //SavePageModel(9);
            //SavePageModel(10);
            //SavePageModel(11);
            //SavePageModel(12);
            //SavePageModel(13);
            //SavePageModel(14);
            //SavePageModel(15);
            //SavePageModel(16);
            //SavePageModel(17);
            //SavePageModel(18);
            //SaveSportPageModel(19);
            //for (int i = 19; i < 33; i++)
            //{
            //    SaveSportPageModel(i);
            //}
            //SimCMS.Common.WebHelper.GetClientWeb(Server.MapPath("./"));
            //var List=  SimCMS.Common.WebHelper.GetCateList("http://www.xinshipu.com/%E8%8F%9C%E8%B0%B1%E5%A4%A7%E5%85%A8.html");
            //foreach (var Cate in List)
            //{
            //    SimFCategoryManager.Save(Cate);
            //}
            //320
            //381 缺失
            //var List = SimFCategoryManager.LoadAll().Where(o => o.Id > 381).ToList();
            //int Count = 0;
            //foreach (var Model in List)
            //{
            //    var NewList = SimCMS.Common.WebHelper.MenuList("http://www.xinshipu.com/" + Model.Url, null, Model.Id, Server.MapPath("./"));
            //    foreach (var NewModel in NewList)
            //    {

            //            SimFoodmenuManager.Save(NewModel);

            //    }

            //}
            //var list = SimFoodmenuManager.LoadAll().Where(o=>o.Id>1199).ToList();
            //foreach (var Model in list)
            //{

            //    var Models = SimCMS.Common.WebHelper.GetfoodMenuDetailWeb(Model, Server.MapPath("./"), "http://www.xinshipu.com/" + Model.Url);
            //    SimFoodmenuManager.Update(Models);
            //}
            //DistinctFoodMenu();
            var list = SimFoodmenuManager.LoadAll();
            foreach (var Model in list)
            {
                if (!System.IO.File.Exists(Server.MapPath("./") + "NewImg/" + Model.Id.ToString() + ".jpg"))
                {
                    if (System.IO.File.Exists(Server.MapPath("./") + Model.Pic))
                    {
                        System.IO.File.Copy(Server.MapPath("./") + Model.Pic, Server.MapPath("./") + "NewImg/" + Model.Id.ToString() + ".jpg");
                    }
                }
            }

            return View();
        }

        int C = 1;
        public void DistinctFoodMenu()
        {
            var List = SimFoodmenuManager.LoadAll().Where(o=>o.Material==null).OrderByDescending(o => o.Url).ToList();
            string GroupName = string.Empty;
            foreach (var Model in List)
            {
                var Models = SimCMS.Common.WebHelper.GetfoodMenuDetailWeb(Model, Server.MapPath("./"), "http://www.xinshipu.com/" + Model.Url);
                    SimFoodmenuManager.Update(Models);                                 
            }
        }


        public void SavePageModel(int Type)
        {
            var AttList = SimobjattrManager.LoadAll().Where(o => o.ObjId == 1).ToList();
            var SAttList = SimobjsattrManager.LoadAll();
            #region food
            //蛋奶食物
            if (Type == 1)
            {
                for (int i = 1; i < 5; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-1-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //谷物薯类 
            else if (Type == 2)
            {
                for (int i = 1; i < 10; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-2-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //禽类 
            else if (Type == 3)
            {
                for (int i = 1; i < 6; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-3-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //畜类 
            else if (Type == 4)
            {
                for (int i = 1; i < 12; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-4-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //汤酱汁类
            else if (Type == 5)
            {
                for (int i = 1; i < 6; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-5-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //水果 
            else if (Type == 6)
            {
                for (int i = 1; i < 4; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-6-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //蔬菜 
            else if (Type == 7)
            {
                for (int i = 1; i < 17; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-7-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //坚果种子
            else if (Type == 8)
            {
                for (int i = 1; i < 4; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-8-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //豆类 
            else if (Type == 9)
            {
                for (int i = 1; i < 6; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-9-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //水产类 
            else if (Type == 10)
            {
                for (int i = 1; i < 9; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-10-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //油脂食物
            else if (Type == 11)
            {
                for (int i = 1; i < 4; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-11-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //香辛草药 
            else if (Type == 12)
            {
                for (int i = 1; i < 9; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-12-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //婴儿食物 
            else if (Type == 13)
            {
                for (int i = 1; i < 7; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-13-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //饮料 
            else if (Type == 14)
            {
                for (int i = 1; i < 13; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-14-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //烘培食品 
            else if (Type == 15)
            {
                for (int i = 1; i < 6; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-15-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //糖果 
            else if (Type == 16)
            {
                for (int i = 1; i < 8; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-16-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //速食食品 
            else if (Type == 17)
            {
                for (int i = 1; i < 5; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-17-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            //小吃零食 
            else if (Type == 18)
            {
                for (int i = 1; i < 13; i++)
                {
                    string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/food/list-18-" + i + ".html");
                    var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 1);
                    SaveFoodDetail(Type, AttList, List);
                }
            }
            #endregion
            #region sport
            #endregion
        }

        private void SaveFoodDetail(int Type, List<Simobjattr> AttList, List<Simchildobj> List)
        {
            foreach (var Model in List)
            {
                var objModels = SimCMS.Common.WebHelper.GetDetailInformation(Model, AttList);
                if (objModels != null)
                {
                    objModels.ObjId = 1;
                    objModels.CType = Type;
                    SimchildobjManager.Save(objModels);
                }
                else
                {
                    string Newstr = SimCMS.Common.WebHelper.CallPage(Model.Url);
                    var NewList = SimCMS.Common.WebHelper.GetModelListByHtml(Newstr, Server.MapPath("./"), 1);
                    foreach (var NewModel in NewList)
                    {
                        var NewobjModels = SimCMS.Common.WebHelper.GetDetailInformation(NewModel, AttList.ToList());
                        if (NewobjModels != null)
                        {
                            NewobjModels.ObjId = 1;
                            NewobjModels.CType = Type;
                            SimchildobjManager.Save(NewobjModels);
                        }
                    }
                }
            }
        }


        public void SaveSportPageModel(int Type)
        {
            var AttList = SimobjattrManager.LoadAll().Where(o => o.ObjId == 2).ToList();
            #region sport
            //日常活动
            if (Type == 19)
            {

                string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/exercise/list-1-1.html");
                var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 2);
                SaveSportDetail(Type, AttList, List);

            }
            //交通工具 
            if (Type == 20)
            {

                string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/exercise/list-2-1.html");
                var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 2);
                SaveSportDetail(Type, AttList, List);

            }
            //体育运动 
            if (Type == 21)
            {

                string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/exercise/list-3-1.html");
                var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 2);
                SaveSportDetail(Type, AttList, List);

            }
            //球类运动 
            if (Type == 22)
            {

                string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/exercise/list-4-1.html");
                var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 2);
                SaveSportDetail(Type, AttList, List);

            }
            //健身器械 
            if (Type == 23)
            {

                string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/exercise/list-5-1.html");
                var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 2);
                SaveSportDetail(Type, AttList, List);

            }
            //舞蹈体操 
            if (Type == 24)
            {

                string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/exercise/list-6-1.html");
                var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 2);
                SaveSportDetail(Type, AttList, List);

            }
            //居家活动 
            if (Type == 25)
            {

                string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/exercise/list-7-1.html");
                var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 2);
                SaveSportDetail(Type, AttList, List);

            }
            //个人护理 
            if (Type == 26)
            {

                string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/exercise/list-8-1.html");
                var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 2);
                SaveSportDetail(Type, AttList, List);

            }
            //音乐活动 
            if (Type == 27)
            {

                string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/exercise/list-9-1.html");
                var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 2);
                SaveSportDetail(Type, AttList, List);

            }
            //钓鱼打猎 
            if (Type == 28)
            {

                string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/exercise/list-10-1.html");
                var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 2);
                SaveSportDetail(Type, AttList, List);

            }
            //水上运动 
            if (Type == 29)
            {

                string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/exercise/list-11-1.html");
                var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 2);
                SaveSportDetail(Type, AttList, List);

            }
            //冬季运动 
            if (Type == 30)
            {

                string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/exercise/list-12-1.html");
                var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 2);
                SaveSportDetail(Type, AttList, List);

            }
            //情侣活动 
            if (Type == 31)
            {

                string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/exercise/list-13-1.html");
                var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 2);
                SaveSportDetail(Type, AttList, List);

            }
            //特殊职业 
            if (Type == 32)
            {

                string str = SimCMS.Common.WebHelper.CallPage("http://fitness.39.net/exercise/list-14-1.html");
                var List = SimCMS.Common.WebHelper.GetModelListByHtml(str, Server.MapPath("./"), 2);
                SaveSportDetail(Type, AttList, List);

            }
            #endregion
        }

        private void SaveSportDetail(int Type, List<Simobjattr> AttList, List<Simchildobj> List)
        {
            foreach (var Model in List)
            {
                var objModels = SimCMS.Common.WebHelper.GetSportDetailInformation(Model, AttList);
                if (objModels != null)
                {
                    objModels.ObjId = 2;
                    objModels.CType = Type;
                    SimchildobjManager.Save(objModels);
                }
                else
                {
                    string Newstr = SimCMS.Common.WebHelper.CallPage(Model.Url);
                    var NewList = SimCMS.Common.WebHelper.GetModelListByHtml(Newstr, Server.MapPath("./"), 2);
                    foreach (var NewModel in NewList)
                    {
                        var NewobjModels = SimCMS.Common.WebHelper.GetSportDetailInformation(NewModel, AttList.ToList());
                        if (NewobjModels != null)
                        {
                            NewobjModels.ObjId = 2;
                            NewobjModels.CType = Type;
                            SimchildobjManager.Save(NewobjModels);
                        }
                    }
                }
            }
        }

    }
}
